package com.backend.springboot_project_demo.controller;

import com.backend.springboot_project_demo.common.Result;
import com.backend.springboot_project_demo.entity.TestTestTest;
import com.backend.springboot_project_demo.entity.User;
import com.backend.springboot_project_demo.entity.UserTransfer;
import com.backend.springboot_project_demo.service.AddUserService;
import com.backend.springboot_project_demo.service.DeleteUserService;
import com.backend.springboot_project_demo.service.SearchUserService;
import com.backend.springboot_project_demo.service.UpdateUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@PreAuthorize("hasAuthority('admin')")
@RestController
@RequestMapping("/user")
public class UserController {
    private @Resource AddUserService addUserService;
    private @Resource SearchUserService searchUserService;
    private @Resource UpdateUserService updateUserService;
    private @Resource DeleteUserService deleteMapping;

    @PostMapping("/page")
    public Result<List<UserTransfer>> getUsersByPage(@RequestBody UserTransfer userTransfer) {
        List<UserTransfer> users = searchUserService.getUsersByPage(userTransfer);
        return Result.success(users);
    }

    @PostMapping("/test")
    public Result<TestTestTest> getTest(@Valid @RequestBody TestTestTest input) {
        TestTestTest test = new TestTestTest("");
        return Result.success(test);
    }

    @GetMapping("/users")
    public Result<List<UserTransfer>> getUsers() {
        List<UserTransfer> users = searchUserService.getUsers();
        return Result.success(users);
    }

    @GetMapping("/{id}")
    public Result<UserTransfer> getUserById(@PathVariable String id) {
        User user = searchUserService.findUserById(id);
        return Result.success(user.toUserTransfer());
    }

    @PostMapping("/add")
    public Result<Boolean> addUser(@RequestBody UserTransfer userTransfer) {
        boolean result = addUserService.addUser(userTransfer.toAddUser());
        return Result.success(result);
    }

    @PutMapping("/update")
    public Result<Boolean> updateUser(@RequestBody UserTransfer userTransfer) {
        boolean result = updateUserService.updateUser(userTransfer.toUpdateUser());
        return Result.success(result);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteUser(@PathVariable String id) {
        boolean result = deleteMapping.deleteUser(id);
        return Result.success(result);
    }
}
